package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.ObjectEncoding;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.text.TextField;
	import flash.utils.ByteArray;

	public class example extends Sprite
	{
		private var remoting:Remoting;
		private var btnPos:Point;
		private var btnCount:uint;
		private var gatewayUrl:String;
		private var postUrl:String;

		public function example()
		{
			this.gatewayUrl = "http://localhost/examples/gateway.php";
			this.postUrl = "http://localhost/examples/post.php";
			this.remoting = new Remoting(this.gatewayUrl, ObjectEncoding.AMF3);
			this.initBtn();
		}

		private function initBtn():void
		{
			this.btnCount = 0;
			this.btnPos = new Point(0, 30);
			this.createBtn("POST", onClick_post);
			this.createBtn("Gateway", onClick_gateway);
		}

		private function createBtn(str:String, func:Function):void
		{
			++this.btnCount;

			var btn:Sprite = new Sprite();
			btn.graphics.beginFill(0xcccccc);
			btn.graphics.drawRect(0, 0, 80, 30);
			btn.graphics.endFill();
			this.addChild(btn);
			if (0 == (this.btnCount - 1) % 5) {
				this.btnPos.x = 0;
				this.btnPos.y = 35 + Math.floor(this.btnCount / 5) * 35;
			}
			btn.x = this.btnPos.x;
			btn.y = this.btnPos.y;
			this.btnPos.x += 85;

			var txt:TextField = new TextField();
			txt.text = str;
			btn.addChild(txt);
			txt.x = 3;
			txt.y = 5;
			txt.selectable = false;

			btn.addEventListener(MouseEvent.CLICK, func);
		}

		public function onClick_post(e:MouseEvent):void
		{
			var request:URLRequest = new URLRequest(this.postUrl);
			request.method = URLRequestMethod.POST;
			var data:URLVariables = new URLVariables();
			var bytes:ByteArray = new ByteArray();
			bytes.objectEncoding = ObjectEncoding.AMF3;
			//bytes.writeByte(0x11);
			bytes.writeObject(
				{ myString: "Hello World" }
			);
			data.args = bytes;
			request.data = data;
			var urlLoader:URLLoader = new URLLoader();
			urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
			urlLoader.addEventListener(Event.COMPLETE, onCompleteHandler);
			urlLoader.load(request);

			function onCompleteHandler(evt:Event):void {
				var bytes:ByteArray = new ByteArray();
				//bytes.shareable = true;
				bytes.objectEncoding = ObjectEncoding.AMF3;
				bytes.writeUTFBytes(evt.target.data);
				bytes.position = 0;
				//bytes.readByte();
				var obj:Object = bytes.readObject();
				for (var what:String in obj) {
					trace(what + ":\"" + obj[what] + "\"");
				}
			}
		}

		public function onClick_gateway(e:MouseEvent):void
		{
			var params:Object = { "openid": 1234567890, "sendMessage": "AmfExt AS3 Example !" };
			this.remoting.respond("ExampleService.Test", onGetData_Result, onGetData_Fault, params);
		}

		private function onGetData_Result(re:*):void
		{
			for (var what:String in fe) {
				trace(what + ":" + fe[what]);
			}
		}

		private function onGetData_Fault(fe:*):void
		{
			trace(re);
		}
	}
}
